<template>
	<view>
		<u-sticky bgColor="#fff">
			<u-navbar :fixed="false" :placeholder="true" title="点检任务" @leftClick="leftClick" bgColor="#0F53AC"
				leftIconColor="#fff">
			</u-navbar>
			<u-tabs class="uTabs" :list="list" lineWidth="80" lineColor="#0F53AC" :activeStyle="activeStyle"
				:scrollable="false" :inactiveStyle="inactiveStyle" :itemStyle="itemStyle" @click="tabsClick"
				:current="current">
			</u-tabs>
			<u-gap height="1" bgColor="#f2f2f2"></u-gap>
			<view class="dropdown" v-if="current==0">
				<zb-dropdown-menu :zIndex="89" @toggleItem="toggleItem(2)" ref="dropdown1" active-color="#0F53AC">
					<zb-dropdown-item :options="option" v-model="value1" @change="change1" name="first">
					</zb-dropdown-item>
					<zb-dropdown-item :options="option2" v-model="value2" @change="change2" name="two">
					</zb-dropdown-item>
				</zb-dropdown-menu>
			</view>
			<view class="dropdown" v-if="current==1">
				<zb-dropdown-menu :zIndex="89" @toggleItem="toggleItem(2)" ref="dropdown1" active-color="#0F53AC">
					<zb-dropdown-item :options="option3" v-model="value3" @change="change3" name="three">
					</zb-dropdown-item>
					<zb-dropdown-item :options="option4" v-model="value4" @change="change4" name="four">
					</zb-dropdown-item>
				</zb-dropdown-menu>
			</view>
		</u-sticky>
		<view class="tab-page" v-if="current==0">
			<view class="item-title">仅显示当天点检任务</view>
			<view class="list-wrapper detail-title-border" v-for="(item,index) in wbList" :key="index">
				<u-gap height="8" bgColor="#f2f2f2" v-if="index > 1"></u-gap>
				<view class="list-info">
					<view class="list-title">
						<view class="title-left">
							点检工单号:{{item.taskOrderNum}}
							<view class="copy" @click="copyOrderNum(item.taskOrderNum)">复制</view>
						</view>
						<view class="title-right"
							:class="[item.checkStatus==0?'title-dyz':item.checkStatus==1?'title-finish':'']">
							{{item.checkStatus==0?'待点检':item.checkStatus==1?'点检中':''}}
						</view>
					</view>
				</view>
				<view class="list-main">
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">设备名称</view>
							<view class="txt2">{{item.deviceName?item.deviceName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">编号</view>
							<view class="txt2">{{item.deviceNum?item.deviceNum:"--"}}</view>
						</view>
					</view>
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">点检类型</view>
							<view class="txt2">{{item.checkTypeName?item.checkTypeName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">点检周期</view>
							<view class="txt2">{{item.period?item.period+item.periodUnit:'--'}}</view>
						</view>
					</view>
					<!-- <view class="gz-desc">
						<view class="title">故障描述</view>
						<view class="desc-reason">fdsfdsfdsffdsfdsf</view>
					</view> -->
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">点检人</view>
							<view class="txt2">{{item.checkUserName?item.checkUserName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">计划开始时间</view>
							<view class="txt2">{{item.planBeginDate}}</view>
						</view>
						<!-- <view class="main-left" v-if="item.checkStatus==0">
							<view class="txt1">计划开始时间</view>
							<view class="txt2">{{item.planBeginDate?item.planBeginDate:"--"}}</view>
						</view>
						<view class="main-left" v-if="item.checkStatus==1">
							<view class="txt1">实际开始时间</view>
							<view class="txt2">{{item.factBeginDate?item.factBeginDate:"--"}}</view>
						</view> -->
					</view>
					<!-- <view class="item-main">
						<view class="main-left">
							<view class="txt1">报修人</view>
							<view class="txt2">张三</view>
						</view>
						<view class="main-left">
							<view class="txt1">报修时间</view>
							<view class="txt2">2022/2/16 20:11:11</view>
						</view>
					</view> -->
				</view>
				<view class="item-btn" @click="toStartweibao(item)">
					<view class="ruku">{{item.checkStatus==0?'开始点检':item.checkStatus==1?'继续点检':''}}</view>
				</view>
			</view>
			<view class="bottom-subside" v-if="total<6"></view>
			<u-loadmore v-if="total>5" :status="status" :line="true" height="200rpx" />

		</view>
		<view class="tab-page" v-if="current==1">
			<view class="item-title">仅展示近三个月已点检任务</view>
			<view class="list-wrapper detail-title-border" v-for="(item,index) in wbList" :key="index">
				<u-gap height="8" bgColor="#f2f2f2" v-if="index > 1"></u-gap>
				<view class="list-info">
					<view class="list-title">
						<view class="title-left">
							工单号:{{item.taskOrderNum}}
							<view class="copy" @click="copyOrderNum(item.taskOrderNum)">复制</view>
						</view>
						<view class="title-right"
							:class="[item.checkStatus==9?'title-close':item.checkStatus==2?'title-finish':item.checkStatus==3?'title-close':'']">
							{{item.checkStatus==9?'取消点检':item.checkStatus==2?'点检完成':item.checkStatus==3?'逾期未点检':''}}
						</view>
					</view>
				</view>
				<view class="list-main">
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">设备名称</view>
							<view class="txt2">{{item.deviceName?item.deviceName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">编号</view>
							<view class="txt2">{{item.deviceNum?item.deviceNum:"--"}}</view>
						</view>
					</view>
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">点检类型</view>
							<view class="txt2">{{item.checkTypeName?item.checkTypeName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">点检周期</view>
							<view class="txt2">{{item.period?item.period+item.periodUnit:'--'}}</view>
						</view>
					</view>
					<!-- <view class="gz-desc">
						<view class="title">故障描述</view>
						<view class="desc-reason">fdsfdsfdsffdsfdsf</view>
					</view> -->
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">点检结果</view>
							<view class="txt2" :class="[item.checkResult==1?'txt-zc':item.checkResult==2?'txt-yc':'']">
								{{item.checkResult==1?'正常':item.checkResult==2?'异常':''}}
							</view>
						</view>
						<view class="main-left">
							<view class="txt1">点检费用</view>
							<view class="txt2">{{item.checkFee?item.checkFee+'元':"--"}}</view>
						</view>
					</view>
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">点检人</view>
							<view class="txt2">{{item.checkUserName?item.checkUserName:"--"}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">点检完成时间</view>
							<view class="txt2">{{item.factEndDate?item.factEndDate:"--"}}</view>
						</view>
					</view>
				</view>
				<view class="item-btn" @click="toSelectDetail(item)">
					<view class="ruku">查看详情</view>
				</view>
			</view>
			<view class="bottom-subside" v-if="total<6"></view>
			<u-loadmore v-if="total>5" :status="status" :line="true" height="200rpx" />
		</view>
		<view class="empty-data" v-if="wbList.length==0">
			<u-empty mode="data" :marginTop="100" text="无点检任务"></u-empty>
		</view>
		<view class="fixed-btn" @click="togoRepairpage" v-if="current==0">
			<view class="btn-icon iconfont icon-app-saoma">
				<!-- <image src="../../../static/images/index/weiwai.png"></image> -->
			</view>
			<view class="btn-txt">快速点检</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: 'loadmore',
				activeStyle: {
					color: '#0F53AC',
					fontWeight: 'bold',
					transform: 'scale(1.05)'
				},
				inactiveStyle: {
					color: '#8c8c8c',
					transform: 'scale(1)'
				},
				itemStyle: {
					paddingLeft: '30rpx',
					paddingRight: '30rpx',
					paddingTop: '24rpx',
					paddingBottom: '24rpx',
					height: '48rpx'
				},
				current: 0,
				list: [{
					name: '待点检任务',
				}, {
					name: '已点检任务',
				}],
				option: [],
				value1: '',
				option2: [{
						text: '全部状态',
						value: ''
					},
					{
						text: '待点检',
						value: 0
					},
					{
						text: '点检中',
						value: 1
					}
				],
				value2: '',
				option3: [],
				option4: [{
						text: '全部状态',
						value: ''
					},
					{
						text: '点检完成',
						value: 2
					},
					{
						text: '逾期未点检',
						value: 3
					},
					{
						text: '取消点检',
						value: 9
					}
				],
				value3: '',
				value4: '',
				wbList: [],
				params: {
					checkedFlag: 0, //是否是已点检任务；0.否； 1.是 必填
					checkStatus: '', //点检状态；0.待执行； 1.执行中； 2.执行完成；9.取消；
					checkType: '', //点检类型
					pn: 1,
					ps: 5,
					fastDeviceNum: ''
				},
				pages: 1,
				total: 0
			}
		},
		onLoad(options) {
			this.getCheckupAllType()
			this.params.fastDeviceNum = ''
			if (options.current) {
				this.current = options.current
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 1;
				this.params.checkStatus = '';
				this.getList();
			} else {
				this.params.pn = 1;
				this.params.checkedFlag = 0;
				this.getList();
			}


		},
		onPullDownRefresh() {
			if (this.current == 0) { //待维保任务
				this.params.pn = 1;
				this.params.checkedFlag = 0;
				this.params.checkStatus = '';
				this.wbList = [];
				this.getList();
			}
			if (this.current == 1) { //已维保任务
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 1;
				this.params.checkStatus = '';
				this.getList();
			}

			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 400)
		},
		methods: {
			leftClick() {
				uni.switchTab({
					url: '/pages/work/index'
				})
			},
			getCheckupAllType() {
				uni.$u.http.post(`/device/mobile/sysDataDictionary/dictionaryList`, {
					configType: 'check_type'
				}).then(res => {
					console.log('res---->', res)
					if (res.code == 0) {
						this.option = [{
							text: '选择点检类型',
							value: ''
						}];
						this.option3 = [{
							text: '选择点检类型',
							value: ''
						}];
						if (res.data && res.data.length > 0) {
							res.data.forEach(item => {
								let dd = {
									text: item.configValue,
									value: item.id
								}
								this.option.push(dd)
								this.option3.push(dd)
							})
							this.value1 = '';
							this.value3 = '';
						}
					}
				}).catch(err => {})
			},
			tabsClick(e) {
				console.log('选择', e)
				if (this.current != e.index) {
					this.params.fastDeviceNum = ''
				}
				this.current = e.index
				if (this.current == 0) { //待维保任务
					this.params.pn = 1;
					this.params.checkedFlag = 0;
					this.params.checkStatus = '';
					this.params.checkType = '';
					this.value1 = ''
					this.value2 = ''
					this.wbList = [];
					this.getList();
				}
				if (this.current == 1) { //已维保任务
					this.wbList = [];
					this.params.pn = 1;
					this.params.checkedFlag = 1;
					this.params.checkStatus = '';
					this.params.checkType = '';
					this.value3 = ''
					this.value4 = ''
					this.getList();
				}

			},
			toggleItem(val) {
				if (val === 1) {
					this.$refs.dropdown1.close()
				}
			},
			change1(val) {
				console.log('===========', val, this.value1)
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 0;
				this.params.checkType = this.value1;
				this.getList();
			},
			change2(val) {
				console.log('===========', val, this.value2)
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 0;
				this.params.checkStatus = this.value2;
				this.getList();
			},
			change3(val) {
				console.log('===========', val, this.value3)
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 1;
				this.params.checkType = this.value3;
				this.getList();
			},
			change4(val) {
				console.log('===========', val, this.value3)
				this.wbList = [];
				this.params.pn = 1;
				this.params.checkedFlag = 1;
				this.params.checkStatus = this.value4;
				this.getList();
			},
			closeRepair() {
				uni.navigateTo({
					url: '/pages/subPackagesB/weiwaiRepair/closerepair'
				})
			},
			togoRepairpage() {
				const that = this
				uni.scanCode({
					onlyFromCamera: true,
					scanType: ['barCode', 'qrCode', 'datamatrix', 'pdf417'],
					autoDecodeCharset: false,
					autoZoom: false,
					barCodeInput: false,
					success: function(res) {
						console.log('条码内容：' + res.result);
						let codeInfo = res.result.split('?')[1]
						//这里获取扫码信息并且对扫码信息进行解析
						let equId = codeInfo.split('&')[0];
						let deviceNum = codeInfo.split('&')[1];
						that.getCheckTaskList(deviceNum.split('=')[1])
					},
					fail: function(error) {
						console.log('扫码失败了')
					}
				});
			},
			getCheckTaskList(deviceNum) {
				this.params.pn = 1
				this.params.fastDeviceNum = deviceNum
				uni.$u.http.post(`/device/mobile/deviceCheckTask/appPageList`, this.params).then(res => {
					console.log(res)
					if (res.code == 0) {
						//跳转到详情页面
						if (res.data.records.length === 1) {
							const {
								id,
								taskOrderNum
							} = res.data.records[0]
							uni.navigateTo({
								url: '/pages/subPackagesB/checkupTask/checkinfo?id=' + id +
									'&taskOrderNum=' + taskOrderNum
							})
						}
						//渲染新的列表
						else if (res.data.records.length > 1) {
							this.wbList = res.data.records
							this.pages = res.data.pages;
							this.total = res.data.total;
						} else {
							uni.$u.toast('当前设备无点检任务!')
						}
					}
				}).catch(err => {})
			},
			toSelectDetail(item) {
				uni.navigateTo({
					url: '/pages/subPackagesB/checkupTask/checkDetail?wbId=' + item.id
				})
			},
			toStartweibao(item) {
				if (item.checkStatus == 0) {
					uni.$u.http.post(`/device/mobile/deviceCheckTask/startCheckTask/${item.id}`).then(res => {
						if (res.code == 0) {}
					}).catch(err => {})
				}
				uni.navigateTo({
					url: '/pages/subPackagesB/checkupTask/checkinfo?id=' + item.id + '&taskOrderNum=' + item
						.taskOrderNum
				})
			},
			getList() {
				uni.$u.http.post(`/device/mobile/deviceCheckTask/appPageList`, this.params).then(res => {
					console.log('res===>列表', res)
					if (res.code == 0) {
						this.wbList = this.wbList.concat(res.data.records);
						this.pages = res.data.pages;
						this.total = res.data.total;
					}
				}).catch(err => {})
			},
			copyOrderNum(data) {
				uni.setClipboardData({
					data: data,
					success: function() {
						console.log('success');
						uni.$u.toast('复制成功')
					}
				});
			}
		},
		onReachBottom() {
			console.log('dfdfd', this.params.pn);
			if (this.params.pn >= this.pages) return;
			this.status = 'loading';
			this.params.pn = ++this.params.pn;
			setTimeout(() => {
				this.getList()
				if (this.params.pn >= this.pages) this.status = 'nomore';
				else this.status = 'loading';
			}, 1000)
		}
	}
</script>

<style lang="scss">
	page {
		background: #ffffff;
	}

	.tab-page {
		margin-top: 2rpx;
	}

	.item-title {
		font-size: 28rpx;
		color: #8c8c8c;
		background: #F5F5F5;
		padding: 28rpx 0;
		text-align: center;
	}

	.dropdown {
		.list2 {
			.sort {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 26rpx;
				color: #000000;
				padding: 20rpx 0;
			}
		}

		.sort-list {
			width: 100%;
			height: 100vh;
			background: #9d9d9d;
			z-index: 22;
			position: absolute;

			.sort-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 40rpx 10rpx 20rpx;
				font-size: 34rpx;
				border-bottom: 1px solid #f2f2f2;
				z-index: 99;
				position: relative;
				background: #fff;
			}

			.sort-item:last-child {
				border-bottom: none;
			}

			.sort-item:first-child {
				border-top: 1px solid #f2f2f2;
			}
		}
	}

	.list-wrapper {
		.list-info {
			margin-top: 10rpx;
			background: #fff;
			padding: 20rpx 40rpx;

			.list-title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title-left {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: #666666;

					.copy {
						color: rgba(166, 166, 166, 1);
						font-size: 20rpx;
						margin-left: 10rpx;
						border-radius: 8rpx;
						background: rgba(166, 166, 166, 0.3);
						padding: 4px 6px 4px 6px;
					}
				}

				.title-right {
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
					border-radius: 8rpx;
					font-size: 24rpx;
					padding: 0 20rpx
				}

				.title-ongoing {
					color: rgba(2, 173, 130, 1);
					background: rgba(2, 173, 130, 0.2);
				}

				.title-dyz {
					color: rgba(15, 83, 172, 1);
					background-color: rgba(15, 83, 172, 0.2);
				}

				.title-close {
					color: rgba(237, 118, 21, 1);
					background-color: rgba(252, 119, 10, 0.2);
				}

				.title-finish {
					color: rgba(2, 173, 130, 1);
					background-color: rgba(2, 173, 130, 0.2);
				}
			}

		}

		.list-main {
			padding: 4rpx 10rpx 20rpx 10rpx;
			background: #fff;
			border-top: 1px solid #f2f2f2;
			border-bottom: 1px solid #f2f2f2;

			.item-main {
				display: flex;
				justify-content: space-between;
				padding: 4rpx 30rpx;

				.main-left {
					width: 50%;
					text-align: left;

					.txt1 {
						color: #8c8c8c;
						font-size: 30rpx;
						padding: 10rpx 0;
					}

					.txt2 {
						color: #4D4D4D;
						font-size: 32rpx;
					}

					.txt-zc {
						color: #02AD82;
					}

					.txt-yc {
						color: #FF4D4D;
					}
				}
			}

			.gz-desc {
				padding: 15rpx 30rpx;

				.title {
					font-size: 28rpx;
					color: #8c8c8c;
				}

				.desc-reason {
					font-size: 28rpx;
					color: #2b2b2b;
					margin-top: 15rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}
		}

		.item-btn {
			display: flex;
			justify-content: space-around;
			align-items: center;
			padding: 24rpx 0;
			background: #fff;

			.chuku {
				width: 45%;
				font-size: 34rpx;
				color: #8c8c8c;
				text-align: center;
			}

			.ruku {
				width: 45%;
				font-size: 34rpx;
				color: #0F53AC;
				text-align: center;
			}

			.su-line {
				width: 4rpx;
				height: 50rpx;
				background: #f2f2f2;
			}
		}
	}

	.fixed-btn {
		position: fixed;
		bottom: 200rpx;
		right: 20rpx;
		z-index: 99;
		width: 160rpx;
		height: 160rpx;
		opacity: 1;
		background: rgba(15, 83, 172, 1);
		box-shadow: 0px 2px 6px rgba(10, 61, 128, 0.4);
		border-radius: 50%;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		flex-direction: column;
		color: #fff;

		.btn-icon {
			margin-top: 20rpx;
			font-size: 60rpx;

			image {
				width: 72rpx;
				height: 72rpx;
			}
		}

		.btn-txt {
			margin-top: 10rpx;
		}
	}

	.bottom-subside {
		width: 100%;
		height: 44rpx;
	}

	.u-empty .u-icon {
		z-index: -1;
	}

	.u-tabs__wrapper__nav__item {
		// padding: 0 !important;
		// width: 50%;
	}
</style>
